﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QldtSdh.DAL.Models;

namespace QldtSdh.BLL
{
    public partial class BizMaySanXuat
    {
        public static void GetStatisCanhBao(string maMaySanXuat, out int quaHan, out int trongHan, out int truocHan, string manv = "")
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var lsCTMDPC = db.PhanCongBaoTriDbSet.Where(e => e.MaNhanVien == manv).Select(e => e.MaChiTietMay).ToList();
                var cb = int.Parse(BizAppConfig.GetDSCanhBaoMobile());
                DateTime dt = DateTime.Now.AddDays(cb);
                var ls = db.LichBaoTriDbSet.Where(e =>
                    e.ChiTietMay.BoPhanMay.MaySanXuat.MaMaySanXuat == maMaySanXuat && e.NgayBaoTri<=dt &&
                    (string.IsNullOrEmpty(manv) ? true : lsCTMDPC.Contains(e.MaChiTietMay)))
                    .ToList();
                trongHan = ls.Count(e => DateTime.Now.Date.Subtract(e.NgayBaoTri.Date).Days <= 0.3 * e.ChuKy && DateTime.Now.Date.Subtract(e.NgayBaoTri.Date).Days >= 0);
                quaHan = ls.Count(e => DateTime.Now.Date.Subtract(e.NgayBaoTri.Date).Days > 0.3 * e.ChuKy);
                truocHan = ls.Count(e => DateTime.Now.Date.Subtract(e.NgayBaoTri.Date).Days < 0);
            }
        }
        public static List<MaySanXuat> GetList()
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.MaySanXuatDbSet.OrderBy(e => e.TenMaySanXuat).ToList();
            }
        }
        public static List<MaySanXuat> GetList(string maCongDoan, CEMS4UG_DHKHContext db = null)
        {
            if (db == null)
            {
                using (db = CEMS4UG_DHKHContext.GetContext())
                {
                    return GetList(maCongDoan, db);
                }
            }
            else
            {
                return db.MaySanXuatDbSet.Where(e => e.MaCongDoanSanXuat == maCongDoan).OrderBy(e => e.TenMaySanXuat).ToList();
            }
        }
    }
}
